package servlets;


import java.io.IOException;
//import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import controllers.Login_Controller;

@WebServlet(name="login", urlPatterns={"/JSP/Sami/login"})
public class login extends HttpServlet {
   
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	
    	//PrintWriter out = response.getWriter();
    	Login_Controller LogControl = new Login_Controller();
    	String login= request.getParameter("login");
    	String password= request.getParameter("password");
    	
    	boolean loginWithSuccess = false;
    	loginWithSuccess = LogControl.Verify(login, password);
    	
    	
	    if (loginWithSuccess == true){
	    	HttpSession session = request.getSession();
	    	session.setAttribute("login", login);
	    	
	    	//out.print("login: " + login +"password: " + password + "Mot de passe correct");
	    	request.getRequestDispatcher("loggedin.jsp").forward(request, response);
	    
	    } else{
	    	
	    	
	    	request.setAttribute("loginWithSuccess", loginWithSuccess);
	    	request.getRequestDispatcher("Home.jsp").forward(request, response);
	    	
	    	//response.sendRedirect("Home.jsp");
	    		
	    }
    }
}
